<template>
  <div class="wrap" v-if="helpList.length>0">
    <div class="item" v-for="(item,index) in helpList" :class="{slider:current==index}" :style="bgStyle" :key="index">
      <img v-if="item.headImgUrl" :src="item.headImgUrl" alt="">
      <img v-else src="/static/default-head.png" alt="">
      <div class="info">
        <div class="name">{{item.nickname}}</div>
        <div class="desc">帮你完成了{{item.cardNumber}}关</div>
      </div>
      <div class="gold">
        <img src="/static/energy.png" alt=""> +30
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["list"],
  data() {
    return {
      current: -1,
      lastCurrent:-1,
      helpList: [],
      timer: null,
      styleList: [
        "color:#6b46a3;background:#bb95f5;",
        "color:#2c5c8a;background:#81c2ff;",
        "color:#e49a30;background:#ffd477;",
        "color:#437337;background:#a2e16f;"
      ],
      bgStyle:"color:#6b46a3;background:#bb95f5;"
    };
  },
  watch: {
    list(arr) {
      if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].played) {
            this.helpList.push(arr[i]);
          }
        }
        this.start();
        setTimeout(()=>{
          this.current = this.lastCurrent;
        },200)
      }
    },
    current(value) {
      console.log(value);
      if (value > this.helpList.length - 1) {
        clearInterval(this.timer);
        this.lastCurrent = value;
        this.current = -1;
      }
    }
  },
  methods: {
    start() {
      if (this.helpList.length > 0) {
        this.timer = setInterval(() => {
          if (this.helpList[this.current]&&this.helpList[this.current]!='') {
            this.helpList[this.current].played = true;
          }
          this.getColor();
          this.current++;
        }, 2000);

      }
    },
    getColor() {
      let x = Math.ceil(Math.random() * 4);
      this.bgStyle = this.styleList[x];
    }
  },
  mounted() {
    // setTimeout(() => {
    //   this.start();
    // }, 200);
  }
};
</script>

<style lang="scss" scoped>
.wrap {
  position: fixed;
  top: 0;
  left: 0;
}
.item {
  width: 430rpx;
  height: 96rpx;
  border: 8rpx solid #fff;
  padding-left: 10rpx;
  border-top-right-radius: 1000rpx;
  border-bottom-right-radius: 1000rpx;
  border-left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-100%);
  transition: all 0.3s ease;
  color:#6b46a3;
  background:#bb95f5;
  img {
    width: 76rpx;
    height: 76rpx;
    border: 6rpx solid #fff;
    border-radius: 50%;
    margin-right: 10rpx;
  }
}
.slider {
  transform: translate(0);
}

.info {
  min-width: 190rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;

  .name {
    width: 6em;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    font-size: 30rpx;
    font-weight: bold;
  }
  .desc {
    font-size: 24rpx;
    color: #fff;
  }
}
.gold {
  display: flex;
  align-items: center;
  font-size: 30rpx;
  font-weight: bold;
  img {
    width: 43rpx;
    height: 43rpx;
    margin-right: 6rpx;
  }
}
</style>
